package com.example.administrator.szb.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.activity.supei.SuPeiDeatilActivity;
import com.example.administrator.szb.adapter.SuPeiAllAdapter;
import com.example.administrator.szb.bean.Messages;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.JsonUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySuPeiActivity extends MVPBaseActivity {
    TextView bar_title;

    private SmartRefreshLayout freshlayout;
    private ListView listView;
    View error_net_ll,noda_ll;
    private LinearLayout ll_error;
    private ImageView iv_error;
    private TextView tv_net_error;
    private TextView tv_tips;
    private Button btn_error;
    private LinearLayout no_data_ll;
    private ImageView iv_no_data;
    private TextView tv_no_data;
    private TextView tv_tips2;
    private Button no_data_btn;
    private List list = new ArrayList();
    private boolean isFirstEnter = true;
    int current_page = 1 ;
    int per_page = 10;
    private SuPeiAllAdapter adapter;
    private int type=0;
    private int classe = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_su_pei);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("我的速配项目");
        noda_ll = findViewById(R.id.noda_ll);
        error_net_ll = findViewById(R.id.error_net_ll);
        freshlayout = (SmartRefreshLayout) findViewById(R.id.freshlayout);
        listView = (ListView) findViewById(R.id.listView);
        listView.setBackgroundColor(Color.parseColor("#F3F3F3"));
        ll_error = (LinearLayout) findViewById(R.id.ll_error);
        iv_error = (ImageView) findViewById(R.id.iv_error);
        tv_net_error = (TextView) findViewById(R.id.tv_net_error);
        tv_tips = (TextView) findViewById(R.id.tv_tips);
        btn_error = (Button) findViewById(R.id.btn_error);
        no_data_ll = (LinearLayout) findViewById(R.id.no_data_ll);
        iv_no_data = (ImageView) findViewById(R.id.iv_no_data);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);
        tv_tips2 = (TextView) findViewById(R.id.tv_tips2);
        no_data_btn = (Button) findViewById(R.id.no_data_btn);
    }

    @Override
    protected void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,SuPeiDeatilActivity.class);
                intent.putExtra("id",((Supeis)list.get(position)).getId());
                startActivity(intent);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            freshlayout.autoRefresh();
        }
        freshlayout.setEnableLoadMore(true);//是否启用上拉加载功能
        freshlayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        freshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int page = current_page+1;
                getSuPei(1,page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page = 1;
                getSuPei(0,1);
            }
        });
        btn_error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                freshlayout.autoRefresh();
                getSuPei(0,1);
            }
        });
    }
    public void getSuPei(final int loadtype, int page){

        hideAll();
        String url = URLAddress.SUPEI_MYLIST;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("page",page);
        map.put("per_page",per_page);
        map.put("user_id", SPUtils.getUserId());
        HttpUtils.post2(activity,url,map ,0,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("\"data\":{}","\"data\":[]");
                str = str.replace("\"data\":\"没有数据\"","\"data\":[]");
                str = str.replace("\"0\":[]","\"0\":{}");
                str = str.replace("\"1\":","\"first\":");
                str = str.replace("\"0\":","\"zero\":");
                Messages messages = JsonUtils.getMessages2(str);

                if (messages.getResult()==1){
                    str = JsonUtils.JsonParse(messages);
                    List l = JSON.parseArray(str, Supeis.class);
                    if (loadtype==0){
                        list.clear();
                        freshlayout.finishRefresh();
                        freshlayout.setEnableLoadMore(true);
                    }else {
                        freshlayout.finishLoadmore();
                        current_page++;
                        if (l.size()<per_page){
                            freshlayout.setEnableLoadMore(false);
                        }
                    }
                    list.addAll(JSON.parseArray(str, Supeis.class));

                    if (list.size()==0){

                        showNodata();
                    }
                    showSuPei();
                }


            }

            @Override
            public void onFailure(int requestCode, String str) {
                freshlayout.finishLoadmore();
                freshlayout.finishRefresh();
                showError();
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new SuPeiAllAdapter(context,list);
        listView.setAdapter(adapter);
    }
    public void showSuPei(){
        adapter.notifyDataSetChanged();

    }
    private void showNodata(){
        freshlayout.setVisibility(View.VISIBLE);
        noda_ll.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.GONE);
        QTLog.e("aaaaa");
    }
    private void hideAll(){
        noda_ll.setVisibility(View.GONE);
        freshlayout.setVisibility(View.VISIBLE);
        listView.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.GONE);
    }
    private void showError(){
        freshlayout.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.VISIBLE);
    }
}
